<template>
	<view>dfdsgdg</view>
</template>
<script>
	import {
		userLogin
	} from "@/utils/utils.js";
	import {
		mapGetters,
		mapMutations,
		mapActions
	} from "vuex";
	import {
		getCreditList
	} from '@/api/my.js'
	import WebSocket from "@/utils/websocket4.js"
	export default {
		data() {
			return {};
		},
		computed: {
			// 使用对象展开运算符将 getter 混入 computed 对象中
			...mapGetters(["token"]),
		},
		onLaunch: async function(e) {
			if (!uni.getStorageSync("token") || !this.token) {
				userLogin(this.init, {
					shareId: "",
				});
				return;
			}
			this.init();
		},
		onShow: function() {
			console.log("App Show");
			let updateManager = uni.getUpdateManager();
			if (!updateManager) {
				return;
			} else {
				//新版本更新
				if (uni.canIUse("getUpdateManager")) {
					//判断当前微信版本是否支持版本更新

					updateManager.onCheckForUpdate(function(res) {
						if (res.hasUpdate) {
							// 请求完新版本信息的回调
							updateManager.onUpdateReady(function() {
								uni.showModal({
									title: "更新提示",
									content: "新版本已经准备好，是否重启应用？",
									success: function(res) {
										if (res.confirm) {
											// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
											updateManager.applyUpdate();
										}
									},
								});
							});
							updateManager.onUpdateFailed(function() {
								uni.showModal({
									// 新的版本下载失败
									title: "已经有新版本了哟~",
									content: "新版本已经上线啦~，请您删除当前小程序，到微信 “发现-小程序” 页，重新搜索打开哦~",
								});
							});
						} else {}
					});
				} else {
					uni.showModal({
						// 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
						title: "溫馨提示",
						content: "当前微信版本过低，部分功能无法使用，请升级到最新微信版本后重试。",
					});
				}
			}
		},
		onHide: function() {
			console.log("App Hide");
		},
		methods: {
			...mapMutations(['setPhoneModelList', 'steShowPrivacy', 'setMessages', 'setTotalCredits']),
			...mapActions(['getAreaOptions']),
			init() {
				this.getPrivacySetting()
				this.setPhoneModelList()
				this.getAreaOptions()
				getCreditList({
					pageSize: 1
				}).then(res => {
					this.setTotalCredits(res.data.totalCredits)
				})
				if (!WebSocket.client) {
					this.initWS()
				}
			},
			getPrivacySetting() {
				if (wx.getPrivacySetting) {
					wx.getPrivacySetting({
						success: (res) => {
							console.log("getPrivacySettinggetPrivacySettinggetPrivacySetting");
							console.log(
								res
							); // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
							if (res.needAuthorization) {
								// 需要弹出隐私协议
								this.steShowPrivacy(true);
							} else {
								// 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
								this.steShowPrivacy(false)
							}
						},
						fail: () => {},
						complete: () => {},
					});
				}
			},
			initWS() {
				WebSocket.subscribe(`/generateCustomImageSuccess`, (res) => {
					console.log('收到消息2：', res)
					let data = JSON.parse(res.body)
					console.log(data);
					this.setMessages(data)
					// 如果是我的定制页，则不出现消息提醒
					const pages = getCurrentPages();
					const currentPage = pages[pages.length - 1];
					console.log('currentPage', currentPage.route == 'pages/myCustom')
					if ((data.customImage.status == 3 ||data.customPhoneCase.status == 3 ) && currentPage.route != 'pages/myCustom') {
						uni.showTabBarRedDot({
							index: 1
						})
						uni.showModal({
							content: '您有一张图片已定制完成，可到我的定制页查看',
							confirmText: '立即查看',
							cancelText: '稍后查看',
							success: function(res) {
								if (res.confirm) {
									uni.switchTab({
										url: '/pages/myCustom'
									})
								}
							}
						});
					}
				})
			},
		},
	};
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
	//公共样式
	@import "@/static/style/comm.scss";

	.slider-range {
		padding-top: 0px !important;
	}

	page {
		background-color: #f6f6f6;
		width: 100%;
		height: 100%;
		font-size: 24rpx;
		box-sizing: border-box;
		color: $uni-text-color;
	}

	view {
		box-sizing: border-box;
	}
</style>